package railRoad.web.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import railRoad.Route;
import railRoad.Schedule;
import railRoad.Station;

public class RouteTag extends TagSupport {

	static final long serialVersionUID = 1L;

	private Integer id_route = null;

	public Integer getId_route() {
		return id_route;
	}

	public void setId_route(Integer id_route) {
		this.id_route = id_route;
	}

	@Override
	public int doStartTag() throws JspException {

		if (id_route != null) {
			try {
				Schedule schedule = Schedule.getInstance();

				Route route = schedule.getRouteById(id_route.intValue());
				Station station = null;

				int size = route.size();
				if (size > 0) {
					StringBuilder builder = new StringBuilder();

					builder.append("<font color=\"red\">");
					builder.append(route.getRouteName());
					builder.append("</font>");

					builder.append(" : ");

					for (int i = 0; i < size; i++) {
						station = route.getStationAt(i);
						builder.append("<font color=\"green\">");
						builder.append(station.getStationName());
						builder.append("</font>");

						if (i != size - 1)
							builder.append(" -> ");
					}//for i

					pageContext.getOut().print(builder.toString());
				}//if size
			} catch (Exception ex) {
				throw new JspTagException("SimpleTag: " + ex.getMessage());
			}//catch
			return SKIP_BODY;
		}//if id_route

		return super.doStartTag();
	}//doStartTag

	@Override
	public int doEndTag() {
		return EVAL_PAGE;
	}
}
